/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.SalesmanSessionBean;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import crm.salesOrderProcessing.session.SalesOrderSessionBean;
import javax.faces.event.ActionEvent;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ViewAllSalesOrderManagedBean {

    private SalesOrderEntity selectedSalesOrder;
    private SalesmanEntity employee;
    @EJB
    SalesOrderSessionBean salesOrderSessionBean;
    @EJB
    SalesmanSessionBean employeeManager;

    /** Creates a new instance of ViewAllSalesOrderManagedBean */
    public ViewAllSalesOrderManagedBean() {
        selectedSalesOrder = new SalesOrderEntity();

    }

    public List<SalesOrderEntity> getSalesOrderList() {

        List<SalesOrderEntity> result = new ArrayList();
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long userId = (Long) request.getSession().getAttribute("userId");
        employee = employeeManager.getSalesmanById(userId);

        result = salesOrderSessionBean.getAllSalesOrders(employee);

        return result;
    }

    public void amendSalesOrder(SalesOrderEntity salesOrder) throws IOException {
        
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesOrder", salesOrder);
        FacesContext.getCurrentInstance().getExternalContext().redirect("amendSalesOrder.xhtml");

    }

    public void viewSalesOrder(SalesOrderEntity salesOrder) throws IOException {
        selectedSalesOrder = salesOrder;
   
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesOrder", selectedSalesOrder);
        FacesContext.getCurrentInstance().getExternalContext().redirect("displaySalesOrder.xhtml");
    }

//    public void amendSalesOrder(SalesOrderEntity salesOrder)throws IOException{
//       selectedSalesOrder = salesOrder; 
//       FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newSalesOrder", selectedSalesOrder);
//       FacesContext.getCurrentInstance().getExternalContext().redirect("amendPurchaseOrder.xhtml");  
//    }
    public SalesOrderEntity getSelectedSalesOrder() {
        return selectedSalesOrder;
    }

    public void setSelectedSalesOrder(SalesOrderEntity selectedSalesOrder) {
        this.selectedSalesOrder = selectedSalesOrder;
    }
}
